//The purpose of this class is to allow the user to enter their breakfast intakes.
package com.mckillion.icare;

//Import the java classes required
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.mckillion.icare.R;
import java.util.Calendar;

//The class extends activity which allows the Android features to be imported.
public class BreakfastScreen extends Activity {
	//Declare the variables required for this class.
	//private variables are used to promote consistency.
	private ImageButton homeButton, backButton, settingsButton;
	private ImageView infoButton;
	private TextView infoText;
	private Button saveButton, photoButton;
	private EditText foodDescription, calarieInput;
	private String description, calaries, currentDate;
	private Calendar cal;
	private File myFile;
	private FileOutputStream fos;
	private OutputStreamWriter myOutwriter;
	
	//overwrite the onCreate method so that variables are instantiated on opening.
	public void onCreate(Bundle savedInstanceState) {
	
		//Call to the super class.
		super.onCreate(savedInstanceState);
	
		//Remove title and notification bars to give it an android feel.
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		//Set the content layout to the main_screen XML file.
    	setContentView(R.layout.breakfast_screen);
    	
    	//make the objects match the objects in the layout file.
    	foodDescription=(EditText)findViewById(R.id.bfast_description);
    	calarieInput=(EditText)findViewById(R.id.enter_cal);
    	homeButton=(ImageButton)findViewById(R.id.home_button);
        backButton=(ImageButton)findViewById(R.id.back_button);
        settingsButton=(ImageButton)findViewById(R.id.settings_button);
        saveButton=(Button)findViewById(R.id.save_button);
        photoButton=(Button)findViewById(R.id.photo_button);
        infoButton=(ImageView)findViewById(R.id.imageView4);
        
        //Set a listener for the information button
        infoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Use a layout inflater to open a pop-up window with the informational text.
				LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
				View popupView = layoutInflater.inflate(R.layout.info_popup, null);  
				final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
			    
			    infoText = (TextView) popupWindow.getContentView().findViewById(R.id.info_text);
			    infoText.setText("This is the breakfast intake screen." + "\n" + "\n" +
			    					"Use the text fields to input your nutritional intake." + "\n" + "\n" +
			    					"The take photo button allows you to capture an image of your food." + "\n" + "\n" +
			    					"When the data is entered press the save data button to record the inputs." 
			    					+ "\n" + "\n" + "Press dismiss to close this message.");
			    
			    //create a dismiss button to close the pop-up window.
			    Button dismiss = (Button)popupView.findViewById(R.id.dismiss_button);
	            dismiss.setOnClickListener(new Button.OnClickListener(){
	            	@Override
	            	public void onClick(View v) {
	            		popupWindow.dismiss();
	            	}});
	             
	            //Put the pop-up window in the centre of the screen
	            popupWindow.showAtLocation(infoButton, Gravity.CENTER, 0, 0); 
	   }});
           
        //Set a click listener for the save button which saves the .txt and .jpg files.
        saveButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {		
				//Provide feedback when the button is pressed.
				saveButton.setBackgroundResource(R.drawable.buttonpressed);
				
				//Create a calendar to get the current date.
				cal = Calendar.getInstance();
				currentDate = cal.get(Calendar.DAY_OF_MONTH) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR);
				
				//Convert the textual inputs to a string.
				description = foodDescription.getText().toString();
				calaries = calarieInput.getText().toString();
				
				try {
					//Create a new file with a unique name which includes the date and meal type.
					myFile = new File("/mnt/sdcard/Download/" + currentDate + "breakfast.txt");
					myFile.createNewFile();
					
					//FOS used to output the created file above.
					fos = new FileOutputStream(myFile);
					
					//Use an output writer to write the data to the file.
					myOutwriter = new OutputStreamWriter(fos);
					myOutwriter.append(description + "\n");
					myOutwriter.append(calaries);
					
					//Close the two output writers.
					myOutwriter.close();
					fos.close();
					
					//Provide feedback to the user.
					Toast.makeText(getBaseContext(),"Breakfast Intake Saved",Toast.LENGTH_SHORT).show();
					
				} catch (Exception e) {
					//If there is an error inform the user.
					Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
				}
		}});
        
        //Create a click listener for the photo button.
        photoButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Provide feedback when the button is pressed.
				photoButton.setBackgroundResource(R.drawable.buttonpressed);
				
				//Start a new intent which opens the camera.
				Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				
				//Create a calendar to get the current date.
				Calendar cal = Calendar.getInstance();
				String currentDate = cal.get(Calendar.DAY_OF_MONTH) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR);
				
				//Create a new file with a unique name which includes the date and meal type.
				File photo = new File("/mnt/sdcard/DCIM/Camera/" + currentDate + "breakfast.jpg");
				
				//Start the activity which saves the image.
				i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
				startActivityForResult(i, 1);
			}
		});
        
        //Set a listener for the home button which starts the HomeScreen class
        homeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(BreakfastScreen.this, HomeScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the back button which starts the IntakeScreen class
        backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(BreakfastScreen.this, IntakeScreen.class);
				startActivity(i);
			}
		});
        
        //Set a listener for the settings button which starts the Settings class
        settingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(BreakfastScreen.this, Settings.class);
				startActivity(i);
			}
		});
	}
}
